// pages/distribution/rankingList/rankingList.js
const backCashService = require('../../../../api/backCashService.js')
const distributionService = require('../../../../api/distributionService.js')
const utils = require('../../../../utils/utils.js')

//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabType: ["本月", "本季", "本年"],
    tabSelected: 0,
    rankingList: [],
    page: 1,
    pageSize: 10,
    pageCount: 1,
    noMore: false,
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
    const that = this
    if (app.globalData.userInfo) {
      if (app.globalData.userInfo.member) {
        that.setData({
          member: app.globalData.userInfo.member,
          mobile: app.globalData.userInfo.member.mobile
        })
        that.handelQueryList(this.data.page);
      } else {
        wx.showToast({
          title: '您还不是会员，请先绑定手机号码成为会员',
          icon: 'none',
          duration: 2000
        })
      }
    } else {
      try {
        wx.showModal({
          title: '提示',
          content: '请登录后查看',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.switchTab({
                url: '../my/my'
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } catch (error) {}
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      rankingList: [],
      mobile: '',
      page: 1,
      pageCount: 1,
      noMore: false
    })
    this.onLoad();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.page < this.data.pageCount) {
      // 如果查询结果数量不为0，说明还有下一页，执行查询操作
      this.setData({
        loading: true
      })
      let page = this.data.page;
      ++page;
      this.handelQueryList(page);
    } else {
      // 没有下一页，提示已经查询完毕
      if (this.data.rankingList.length > 0) {
        this.setData({
          noMore: true
        })
      } else {
        wx.showToast({
          title: '没有更多数据~',
          icon: 'none',
          duration: 2000
        })
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //切换顶部标签
  switchTab: function (e) {
    console.log(e)
    this.setData({
      tabSelected: e.currentTarget.dataset.idx,
      rankingList:[]
    });
    //如果需要加载数据
    this.handelQueryList(1);
  },
  //swiperChange
  bindChange: function (e) {
    var that = this;
    this.setData({
      tabSelected: e.detail.current,
      rankingList:[]
    });
    //如果需要加载数据
    this.handelQueryList(1);
  },
  /**
   * 分页查询提现明细信息
   * @param page 当前页码
   */
  handelQueryList: function (page) {
    const that = this
    let date = new Date()
    let now = utils.formatTime(date).replace(/\//g, '-')
    console.log(date.getDate(), now)
    if (that.data.tabSelected === 0) {
      let startDate = now.slice(0, 8) + '01 00:00:00'
      let endDate = now
      that.queryRank(startDate, endDate, page)
    } else if (that.data.tabSelected === 1) {
      let nowMonth = date.getMonth()
      let month = utils.timeFormat(that.getQuarterStartMonth(nowMonth))
      let startDate = now.slice(0, 4) + '-' + month + '-01 00:00:00'
      let endDate = now
      that.queryRank(startDate, endDate, page)
    } else if (that.data.tabSelected === 2) {
      let startDate = now.slice(0, 4) + '-01-01 00:00:00'
      let endDate = now
      that.queryRank(startDate, endDate, page)
    } else {
      let startDate = now.slice(0, 8) + '01 00:00:00'
      let endDate = now
      that.queryRank(startDate, endDate, page)
    }
    let memberId = app.globalData.userInfo.member.id
    distributionService.queryGroup(memberId).then(res => {
      console.log(res)
      if (res && res.length >= 0) {
        let tempGroupList = res;
        this.setData({
          groupList: tempGroupList
        })
      }
      if (this.data.loading) {
        that.setData({
          loading: false
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      })
      if (this.data.loading) {
        that.setData({
          loading: false
        })
      }
    })
  },
  /**
   * 查询榜单
   * @param {string} startDate 
   * @param {string} endDate 
   */
  queryRank: function (startDate, endDate, page) {
    distributionService.queryRank(this.data.member.id, page, this.data.pageSize, startDate, endDate, false).then(res => {
      console.log(res)
      let tempList = []
      if (res & res.records.length > 0) {
        for (let i = 0; i < 3; i++) {
          const ele = res.records[i];
          if (ele) {
            tempList.push(item)
          } else {
            tempList.push({})
          }
        }
      } else {
        tempList = [{}, {}, {}]
      }
      let pageCount = parseInt(res.recordCount / res.pageSize) + 1;
      this.setData({
        page: res.page,
        pageCount: pageCount,
        rankingList: tempList
      })
      if (this.data.loading) {
        that.setData({
          loading: false
        })
      }
    }).catch(err => {
      console.log(err)
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      })
      if (this.data.loading) {
        that.setData({
          loading: false
        })
      }
    })
  },
  // 查询本季度开始时间
  getQuarterStartMonth: function (nowMonth) {
    var quarterStartMonth = 0;
    if (nowMonth < 3) {
      quarterStartMonth = 0;
    }
    if (2 < nowMonth && nowMonth < 6) {
      quarterStartMonth = 3;
    }
    if (5 < nowMonth && nowMonth < 9) {
      quarterStartMonth = 6;
    }
    if (nowMonth > 8) {
      quarterStartMonth = 9;
    }
    return quarterStartMonth;
  },
})